<?php

namespace paymch\models;

use Yii;
use yii\db\Exception;
use common\models\shop\ShopOrder;
use common\models\shop\ShopProductStats;
use common\models\member\MemberBond;
use common\models\member\MemberAccount;
use common\logics\CommissionLogic;
use common\models\user\UserCoupon;

/**
 * 交易成功后异步通知业务处理
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
trait BusinessNotifyTrait {

    /**
     * 商城业务更新
     */
    protected function notifyToShop(&$msg) {
        $trade = $this->trade;
        $busorder = ShopOrder::findOne([
            'order_no' => $trade->bizno
        ]);
        if ($busorder == null) {
            $msg = '商城业务订单【' . $trade->bizno . '】不存在！';
            return false;
        }

        $busorder->trade_id = $trade->id;
        $busorder->out_trade_no = $trade->trade_no;
        $busorder->pay_time = $trade->updated_at;
        $busorder->pay = $trade->total;
        if ($busorder->status == ShopOrder::STATUS_DEFAULT || $busorder->status == ShopOrder::STATUS_ACTIVE) {
            $busorder->status = ShopOrder::STATUS_PAID;
        }
        if (!$busorder->save()) {
            Yii::error($busorder->getErrorMsg(), 'payment');
            $msg = '商城业务订单保存失败！';
            return false;
        }
        //商城订单商品
        if (!empty($busorder->goods)) {
            foreach ($busorder->goods as $goods) {
                $statistic = ShopProductStats::findOrNew($goods->product_id);
                $statistic->weights += ShopProductStats::WIDGETS_PAY;
                $statistic->sales = intval($statistic->sales + rand(1, 3) * pi());
                if (!$statistic->save()) {
                    $msg .= "\n商城订单货品统计信息保存失败。";
                    Yii::error($statistic->getErrorMsg(), 'payment');
                }
            }
        }
        //失效优惠券
        $coupons = $busorder->usedCoupons;
        if (!empty($coupons)) {
            foreach ($coupons as $coupon) {
                $coupon->status = UserCoupon::STATUS_USED;
                if (!$coupon->save()) {
                    $msg .= "\n优惠券使用保存失败。";
                    Yii::error($coupon->getErrorMsg(), 'payment');
                }
            }
        }
        return true;
    }

    /**
     * 通知会员卡支付成功
     */
    protected function notifyToBond(&$msg) {
        $trade = $this->trade;
        $busorder = MemberBond::findOne([
            'order_no' => $trade->bizno
        ]);
        if ($busorder == null) {
            $msg = '保证金业务订单【' . $trade->bizno . '】不存在！';
            Yii::error($msg, 'payment');
            return false;
        }
        $busorder->trade_id = $trade->id;
        $busorder->out_trade_no = $trade->trade_no;
        $busorder->pay_time = $trade->updated_at;
        if ($busorder->status == MemberBond::STATUS_DEFAULT || $busorder->status == MemberBond::STATUS_ACTIVE) {
            $busorder->status = MemberBond::STATUS_PAID;
        }

        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!$busorder->save()) {
                throw new Exception($busorder->getErrorMsg);
            }
            $member = $busorder->member;
            if (!$member->payment()) {
                throw new Exception($member->getErrorMsg);
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            $msg = '会员卡业务订单保存失败！' . $e->getMessage();
            Yii::error($msg . "\n" . VarDumper::export($this->attributes), 'payment');
            return false;
        }
        CommissionLogic::openAccount($busorder->member_id);
        return true;
    }

}
